<html>
<script src="/js/jquery-2.1.1.min.js" type="text/javascript"></script>
<script src="/js/ckeditor/ckeditor.js" type="text/javascript"></script>
<link href="/js/ckeditor/contents.css" type="text/css" rel="stylesheet">
<script src="/js/bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
<link href="/js/bootstrap/css/bootstrap.min.css" type="text/css" rel="stylesheet">
<script src="/js/bootstrap-treeview/dist/bootstrap-treeview.min.js" type="text/javascript"></script>
<link href="/js/bootstrap-treeview/dist/bootstrap-treeview.min.css" type="text/css" rel="stylesheet">
<script src="/js/bootStrap-addTabs/bootstrap.addtabs.min.js" type="text/javascript"></script>
<link href="/js/bootStrap-addTabs/bootstrap.addtabs.css" type="text/css" rel="stylesheet">
<script src="/js/toastr/toastr.min.js" type="text/javascript"></script>
<link href="/js/toastr/toastr.css" type="text/css" rel="stylesheet">
<script src="/js/bootstrap-table/dist/bootstrap-table.min.js" type="text/javascript"></script>
<link href="/js/bootstrap-table/dist/bootstrap-table.min.css" type="text/css" rel="stylesheet">
<script src="/js/bootstrap-table/dist/locale/bootstrap-table-zh-CN.min.js" type="text/javascript"></script>
<script src="/js/bootstrap-fileinput/js/fileinput.min.js" type="text/javascript"></script>
<link href="/js/bootstrap-fileinput/css/fileinput.min.css" type="text/css" rel="stylesheet">
<script src="/js/bootstrap-fileinput/js/locales/zh.js" type="text/javascript"></script>
<script src="/js/bootstrap-dialog/dist/js/bootstrap-dialog.min.js" type="text/javascript"></script>
<link href="/js/bootstrap-dialog/dist/css/bootstrap-dialog.min.css" type="text/css" rel="stylesheet">
<script src="/js/zTreev3/js/jquery.ztree.all.js" type="text/javascript"></script>
<link href="/js/zTreev3/css/zTreeStyle/zTreeStyle.css" type="text/css" rel="stylesheet">
<style>
    .container-fluid {
        margin-top: 20px;
    }
</style>
<head>
    <title>系统菜单管理</title>
    <SCRIPT type="text/javascript">
        var setting = {
            data: {
                simpleData: {
                    enable: true
                }
            }, async: {
                enable: true,
                url: "/Resources/showresource"
            }, view: {
                addHoverDom: function (treeId, treeNode) {
                    var aObj = $("#" + treeNode.tId + "_a");
                    if (treeNode.editNameFlag || $("#btnGroup" + treeNode.tId).length > 0) return;
                    var s = '<span id="btnGroup' + treeNode.tId + '">';
                    if (treeNode.level == 0) {
                        s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="addNode(' + treeNode.id + ')" href="#" >添加</a>';
                    } else if (treeNode.level == 1) {
                        s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;"  onclick="editNode(' + treeNode.id + ')" href="#" title="修改权限信息">修改</a>';
                        if (treeNode.children == undefined) {
                            s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="deleteNode(' + treeNode.id + ')" href="#" >删除</a>';
                        }
                        s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="addNode(' + treeNode.id + ')" href="#" >添加</a>';
                    } else if (treeNode.level == 2) {
                        s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;"  onclick="editNode(' + treeNode.id + ')" href="#" title="修改权限信息">修改</a>';
                        s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="deleteNode(' + treeNode.id + ')" href="#">删除</a>';
                    }
                    s += '</span>';
                    aObj.after(s);
                },
                removeHoverDom: function (treeId, treeNode) {
                    $("#btnGroup" + treeNode.tId).remove();
                }
            }
        };
        $(document).ready(function () {
            $.fn.zTree.init($("#treeDemo"), setting);
        });

        //添加数据
        function addNode(pm) {
            BootstrapDialog.show({
                title: '添加',
                message: function (dialog) {
                    return $("<div></div>").load("/Resources/tzadd");
                },
                type: BootstrapDialog.TYPE_PRIMARY,
                size: BootstrapDialog.SIZE_NORMAL,
                closeable: true,
                buttons: [{
                    label: '添加',
                    cssClass: 'btn btn-primary',
                    action: function (dialog) {
                        if ($("[name = resName]").val() == null || $("[name = resName]").val() == "") {
                            toastr.error("菜单名称不能为空");
                            return;
                        }
                        if ($("[name = url]").val() == null || $("[name = url]").val() == "") {
                            toastr.error("菜单路径不能为空");
                            return;
                        }
                        $.post("/Resources/add", $("#forms").serialize() + "&pid=" + pm, function (pm) {
                            if (pm == 1) {
                                toastr.success("添加成功")
                                dialog.close();
                                $.fn.zTree.init($("#treeDemo"), setting);
                                return
                            }
                            toastr.error("添加失败");
                        }, "text")
                    }
                }, {
                    label: '关闭',
                    action: function (dialog) {
                        dialog.close();
                    }
                }]
            });
        }

        //修改数据
        function editNode(pm) {
            BootstrapDialog.show({
                title: '修改',
                message: function (dialog) {
                    return $("<div></div>").load("/Resources/tzupdate?resId=" + pm);
                },
                type: BootstrapDialog.TYPE_PRIMARY,
                size: BootstrapDialog.SIZE_NORMAL,
                closeable: true,
                buttons: [{
                    label: '修改',
                    cssClass: 'btn btn-primary',
                    action: function (dialog) {
                        if ($("[name = resName]").val() == null || $("[name = resName]").val() == "") {
                            toastr.error("菜单名称不能为空");
                            return;
                        }
                        if ($("[name = url]").val() == null || $("[name = url]").val() == "") {
                            toastr.error("菜单路径不能为空");
                            return;
                        }
                        $.get("/Resources/update", $("#forms").serialize(), function (pm) {
                            if (pm == 1) {
                                toastr.info("修改成功")
                                dialog.close();
                                $.fn.zTree.init($("#treeDemo"), setting);
                            } else
                                toastr.error("修改失败");
                        }, "text")
                    }
                }, {
                    label: '关闭',
                    action: function (dialog) {
                        dialog.close();
                    }
                }]
            });
        }

        //删除数据
        function deleteNode(pm) {
            if (confirm("你确定要删除吗")) {
                $.get("/Resources/delete", {resId: pm}, function (ppm) {
                    if (ppm == 1) {
                        toastr.error("删除成功");
                        $.fn.zTree.init($("#treeDemo"), setting);
                        return;
                    }
                    toastr.error("删除失败");
                }, "text")
            }
        }
    </SCRIPT>
</head>
<body>
<div class="zTreeDemoBackground left">
    <ul id="treeDemo" class="ztree"></ul>
</div>
</body>
</html>
